#!/bin/sh
export SYSTEM=system-application-1.0-SNAPSHOT.jar
export USER=user-application-1.0-SNAPSHOT.jar
export SHOP=shop-application-1.0-SNAPSHOT.jar
export PRODUCT=product-application-1.0-SNAPSHOT.jar
export ORDER=order-application-1.0-SNAPSHOT.jar
export PAY=pay-application-1.0-SNAPSHOT.jar
export PROMOTION=promotion-application-1.0-SNAPSHOT.jar
export GATEWAY=gateway-2.0.6.RELEASE.jar
export RESOURCE=resource-application-1.0-SNAPSHOT.jar
export SYSTEM_port=25601
export USER_port=25602
export SHOP_port=25603
export PRODUCT_port=25604
export ORDER_port=25605
export PAY_port=25606
export PROMOTION_port=25607
export GATEWAY_port=9527
export RESOURCE_port=25609

case "$1" in
start)
        cd  ./mallOfSOA/onemall/
        git pull
        mvn clean  install  -Dmaven.test.skip=true

        P_ID=`ps -ef | grep -w $SYSTEM | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===SYSTEM process not exists or stop success"
        else
            kill -9 $P_ID
            echo "SYSTEM killed success"
        fi

 		    P_ID=`ps -ef | grep -w $USER | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===USER process not exists or stop success"
        else
            kill -9 $P_ID
            echo "USER killed success"
        fi
 		    P_ID=`ps -ef | grep -w $SHOP | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===SHOP process not exists or stop success"
        else
            kill -9 $P_ID
            echo "SHOP killed success"
        fi
 		    P_ID=`ps -ef | grep -w $PRODUCT | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===PRODUCT process not exists or stop success"
        else
            kill -9 $P_ID
            echo "PRODUCT killed success"
        fi
 		    P_ID=`ps -ef | grep -w $ORDER | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===ORDER process not exists or stop success"
        else
            kill -9 $P_ID
            echo "ORDER killed success"
        fi
 		    P_ID=`ps -ef | grep -w $PAY | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===PAY process not exists or stop success"
        else
            kill -9 $P_ID
            echo "PAY killed success"
        fi
 		    P_ID=`ps -ef | grep -w $PROMOTION | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===PROMOTION process not exists or stop success"
        else
            kill -9 $P_ID
            echo "PROMOTION killed success"
        fi
		    P_ID=`ps -ef | grep -w $GATEWAY | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===GATEWAY process not exists or stop success"
        else
            kill -9 $P_ID
            echo "GATEWAY killed success"
        fi
 		    P_ID=`ps -ef | grep -w $RESOURCE | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===RESOURCE process not exists or stop success"
        else
            kill -9 $P_ID
            echo "RESOURCE killed success"
        fi
        echo "===stop success==="

        find   ./  -name '*application*.jar'  |  xargs  -I  '{}'  mv  {}  /app/onemall/
        cd /app/onemall
        ## 启动SYSTEM
        echo "--------SYSTEM 开始启动--------------"
        nohup java -jar $SYSTEM >/app/log 2>&1 &
        SYSTEM_pid=`lsof -i:$SYSTEM_port|grep "LISTEN"|awk '{print $2}'`
        until [ -n "$SYSTEM_pid" ]	
            do
              SYSTEM_pid=`lsof -i:$SYSTEM_port|grep "LISTEN"|awk '{print $2}'`  
            done
        echo "SYSTEM pid is $EUREKA_pid" 
        echo "--------SYSTEM 启动成功--------------"
  		## 启动SHOP
        echo "--------开始启动SHOP---------------"
        nohup java -jar $SHOP >/app/log 2>&1 &
        SHOP_pid=`lsof -i:$SHOP_port|grep "LISTEN"|awk '{print $2}'` 
        until [ -n "$SHOP_pid" ]
            do
              SHOP_pid=`lsof -i:$SHOP_port|grep "LISTEN"|awk '{print $2}'`  
            done
        echo "SHOP pid is $SHOP_pid"     
        echo "---------SHOP 启动成功-----------"

        ## 启动RESOURCE
        echo "--------开始启动RESOURCE---------------"
        nohup java -jar $RESOURCE >/app/log 2>&1 &
        RESOURCE_pid=`lsof -i:$RESOURCE_port|grep "LISTEN"|awk '{print $2}'`
        until [ -n "$RESOURCE_pid" ]
            do
              RESOURCE_pid=`lsof -i:$RESOURCE_port|grep "LISTEN"|awk '{print $2}'`
            done
        echo "RESOURCE pid is $RESOURCE_pid"
        echo "---------RESOURCE 启动成功-----------"


        ## 启动USER
        echo "--------开始启动USER---------------"
        nohup java -jar $USER >/app/log 2>&1 &
        USER_pid=`lsof -i:$USER_port|grep "LISTEN"|awk '{print $2}'` 
        until [ -n "$USER_pid" ]
            do
              USER_pid=`lsof -i:$USER_port|grep "LISTEN"|awk '{print $2}'`  
            done
        echo "启动USER pid is $USER_pid"
        echo "---------启动USER 启动成功-----------"

        ## 启动PRODUCT
        echo "--------开始启动PRODUCT---------------"
        nohup java -jar $PRODUCT >/app/log 2>&1 &
        PRODUCT_pid=`lsof -i:$PRODUCT_port|grep "LISTEN"|awk '{print $2}'` 
        until [ -n "$PRODUCT_pid" ]
            do
              PRODUCT_pid=`lsof -i:$PRODUCT_port|grep "LISTEN"|awk '{print $2}'`  
            done
        echo "PRODUCT pid is $PRODUCT_pid"     
        echo "---------PRODUCT 启动成功-----------"
        ## 启动PROMOTION
        echo "--------开始启动PROMOTION---------------"
        nohup java -jar $PROMOTION >/app/log 2>&1 &
        PROMOTION_pid=`lsof -i:$PROMOTION_port|grep "LISTEN"|awk '{print $2}'` 
        until [ -n "$PROMOTION_pid" ]
            do
              PROMOTION_pid=`lsof -i:$PROMOTION_port|grep "LISTEN"|awk '{print $2}'`  
            done
        echo "PROMOTION pid is $PROMOTION_pid"     
        echo "---------PROMOTION 启动成功-----------"
         ## 启动PAY
        echo "--------开始启动PAY---------------"
        nohup java -jar $PAY >/app/log 2>&1 &
        PAY_pid=`lsof -i:$PAY_port|grep "LISTEN"|awk '{print $2}'` 
        until [ -n "$PAY_pid" ]
            do
              PAY_pid=`lsof -i:$PAY_port|grep "LISTEN"|awk '{print $2}'`  
            done
        echo "PAY pid is $PAY_pid"     
        echo "---------PAY 启动成功-----------"
        
        ## 启动ORDER
        echo "--------开始启动ORDER---------------"
        nohup java -jar $ORDER >/app/log 2>&1 &
        ORDER_pid=`lsof -i:$ORDER_port|grep "LISTEN"|awk '{print $2}'` 
        until [ -n "$ORDER_pid" ]
            do
              ORDER_pid=`lsof -i:$ORDER_port|grep "LISTEN"|awk '{print $2}'`  
            done
        echo "ORDER pid is $ORDER_pid"     
        echo "---------ORDER 启动成功-----------"

       
        ## 启动gateway
        echo "--------开始启动GATEWAY---------------"
        nohup java -jar $GATEWAY >/app/log 2>&1 &
        GATEWAY_pid=`lsof -i:$GATEWAY_port|grep "LISTEN"|awk '{print $2}'`
        until [ -n "$GATEWAY_pid" ]
            do
              GATEWAY_pid=`lsof -i:$GATEWAY_port|grep "LISTEN"|awk '{print $2}'`  
            done
        echo "GATEWAY pid is $GATEWAY_pid"    
        echo "---------GATEWAY 启动成功-----------"
 
 		
        echo "===startAll success==="
        ;;
 stop)
        P_ID=`ps -ef | grep -w $SYSTEM | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===SYSTEM process not exists or stop success"
        else
            kill -9 $P_ID
            echo "SYSTEM killed success"
        fi

 		P_ID=`ps -ef | grep -w $USER | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===USER process not exists or stop success"
        else
            kill -9 $P_ID
            echo "USER killed success"
        fi        
 		P_ID=`ps -ef | grep -w $SHOP | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===SHOP process not exists or stop success"
        else
            kill -9 $P_ID
            echo "SHOP killed success"
        fi
 		P_ID=`ps -ef | grep -w $PRODUCT | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===PRODUCT process not exists or stop success"
        else
            kill -9 $P_ID
            echo "PRODUCT killed success"
        fi
 		P_ID=`ps -ef | grep -w $ORDER | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===ORDER process not exists or stop success"
        else
            kill -9 $P_ID
            echo "ORDER killed success"
        fi
 		P_ID=`ps -ef | grep -w $PAY | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===PAY process not exists or stop success"
        else
            kill -9 $P_ID
            echo "PAY killed success"
        fi
 		P_ID=`ps -ef | grep -w $PROMOTION | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===PROMOTION process not exists or stop success"
        else
            kill -9 $P_ID
            echo "PROMOTION killed success"
        fi
		 P_ID=`ps -ef | grep -w $GATEWAY | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===GATEWAY process not exists or stop success"
        else
            kill -9 $P_ID
            echo "GATEWAY killed success"
        fi
 		P_ID=`ps -ef | grep -w $RESOURCE | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===RESOURCE process not exists or stop success"
        else
            kill -9 $P_ID
            echo "RESOURCE killed success"
        fi                                                        
        echo "===stop success==="
        ;;   
 
restart)
        $0 stop
        sleep 2
        $0 start
        echo "===restart success==="
        ;;   
esac	
exit 0